package com.sysoa.common;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.sysoa.dump.DumpMySql;
import com.sysoa.model.data.RecOrganiz;
import com.sysoa.news.model.AppModel;
import com.sysoa.supervise.model.SuperviseInfo;
import com.sysoa.supervise.model.SuperviseProgressInfo;
import com.sysoa.user.model.User;
import com.sysoa.user.model.UserLockIp;
import com.sysoa.user.model.UserOrganizModel;
import com.sysoa.user.model.UserOrganizUserModel;
import com.sysoa.util.ConvertSwf;
import com.sysoa.util.IpHelper;
import com.sysoa.util.MD5Util;
import com.sysoa.util.QPropertiesUtil;
import com.sysoa.util.QStringUtil;

@Clear
public class CommonController extends Controller {

	public void index() {
		forwardAction("/goindex");
	}
	
	public void iplogin(){
		//判断登录的ip是否符合要求
		//获得客户机的ip
		String ip = IpHelper.getIpAddr(getRequest());
		
		User u = IPCheck(ip);
		
		//访问的ip有绑过帐号则直接登录
		if(null != u){
			setSessionAttr("ipUser", u);
			forwardAction("/_login");
		}else{
			//renderHtml("<h1 align='center' style='color: red;'>无权访问！！</h1>");
			render("err.jsp");
		}
	}

	public void goindex() {

		if (getSession().getAttribute("username") == null) {

				forwardAction("/_l_3");
			
		} else if (Contans.URL_LOACTION_RECP.equals(getSessionAttr("submitType"))) {
			if (Contans.ROLE_USER.equals(getSessionAttr("flag"))) {

				redirect("/goRecpUserIndex");

			} else if (Contans.ROLE_MANAGER.equals(getSessionAttr("flag"))) {

				redirect("/goRecpManagerIndex");
			}

		} else if (Contans.URL_LOACTION_SUPERVISE.equals(getSessionAttr("submitType"))) {

			redirect("/goSupervises");

		} else if (Contans.URL_LOACTION_NEWS.equals(getSessionAttr("submitType"))) {
			
			redirect("/goNews1User");

		} else if (Contans.URL_LOACTION_INTEGRATED.equals(getSessionAttr("submitType"))) {

			redirect("/gointegratedindex");

		} else {
			render("login.jsp");
		}
	}

	public void _l_1() {

		render("login.jsp");

	}

	public void _l_2() {

		render("login2.jsp");

	}

	public void _l_3() {

		render("login3.jsp");

	}

	public void _l_4() {

		render("login4.jsp");

	}

	public void login() {
		User user = User.dao.getUserByLoginName(getPara("username"));
		if (user == null || !user.getStr("password").equals(MD5Util.MD5(getPara("password")))) {
			setAttr("msg", "用户名或密码错误");

			if (Contans.URL_LOACTION_RECP.equals(getPara("submit_type"))) {
				render("login.jsp");
			} else if (Contans.URL_LOACTION_NEWS.equals(getPara("submit_type"))) {
				render("login2.jsp");
			} else if (Contans.URL_LOACTION_SUPERVISE.equals(getPara("submit_type"))) {
				render("login3.jsp");
			} else if (Contans.URL_LOACTION_INTEGRATED.equals(getPara("submit_type"))) {
				render("login4.jsp");
			}
			return;
		} else {
			boolean f1 = true;
			//获得客户机的ip
			String ip = IpHelper.getIpAddr(getRequest());
			UserLockIp ip_ = new UserLockIp();
			ip_.setIp(ip);
			//领导用户登录需要ip验证
			if(Contans.ROLE_JUZHANG.equals(user.getStr("user_role_login_url"))){
				/*List<String> ls = MyXMLReader2DOM4J.getIPs();
				if(!ls.contains(ip)){
					f1 = false;
				};*/
				List<UserLockIp> ips = UserLockIp.dao.findbyuserid(user.getInt("id"));
				if(ips == null || !ips.contains(ip_) ){
					f1 = false;
				}
			
			}
			if(f1){
				Integer oragnizid = UserOrganizUserModel.dao.findByUserid(user.getInt("id")).getInt("user_organiz_id");
				getSession().setAttribute("userId", user.getInt("id"));
				getSession().setAttribute("username", user.getStr("name"));
				getSession().setAttribute("loginname", user.getStr("loginName"));
				getSession().setAttribute("submitType", getPara("submit_type"));
				getSession().setAttribute("employer", user.getStr("user_employer_name"));
				getSession().setAttribute("organiz", user.getStr("user_organiz_name"));
				getSession().setAttribute("organizid", oragnizid);
				getSession().setAttribute("organizidflage", oragnizid+",");
				getSession().setAttribute("role", user.getStr("user_role_name"));
				getSession().setAttribute("flag", user.getStr("user_role_login_url"));
				getSession().setAttribute("user", user);
				getSession().setAttribute("organizList", UserOrganizModel.dao.findAll());
	
				redirect("/goindex");
			}else{
				setAttr("msg", "登录ip异常");
				render("login3.jsp");
			}
		}
	}
	/**
	 * 特殊ip直接登录
	 */
	public void _login() {
		User u = getSessionAttr("ipUser");
		User user = User.dao.getUserByLoginName(u.getStr("loginName"));
		Integer oragnizid = UserOrganizUserModel.dao.findByUserid(user.getInt("id")).getInt("user_organiz_id");
		
		getSession().setAttribute("submitType", Contans.URL_LOACTION_SUPERVISE);
		getSession().setAttribute("userId", user.getInt("id"));
		getSession().setAttribute("username", user.getStr("name"));
		getSession().setAttribute("loginname", user.getStr("loginName"));
		getSession().setAttribute("employer", user.getStr("user_employer_name"));
		getSession().setAttribute("organiz", user.getStr("user_organiz_name"));
		getSession().setAttribute("organizid", oragnizid);
		getSession().setAttribute("organizidflage", oragnizid + ",");
		getSession().setAttribute("role", user.getStr("user_role_name"));
		getSession().setAttribute("flag", user.getStr("user_role_login_url"));
		getSession().setAttribute("user", user);
		getSession().setAttribute("organizList", UserOrganizModel.dao.findAll());

		redirect("/goindex");

	}
	/**
	 * 验证访问ip
	 * 
	 * @param ip
	 * @return  返回用户信息
	 */
	protected static User IPCheck(String ip){
		User u = null;
	
		List<UserLockIp> ls = UserLockIp.dao.findbyip(ip);
       
		if(ls.size()>0){
        	
          Integer uid = ls.get(0).getUserid();
          
          u = User.dao.findById(uid);
        }		
		return u;
	}

	public void goRecpApply() {
		setAttr("ServerTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		render("RecpApply.jsp");
	}

	public void exit() {

		getSession().invalidate();
		
		if (Contans.URL_LOACTION_RECP.equals(getSessionAttr("submitType"))) {
			redirect("/_l_1");
		} else if (Contans.URL_LOACTION_NEWS.equals(getSessionAttr("submitType"))) {
			redirect("/_l_2");
		} else if (Contans.URL_LOACTION_SUPERVISE.equals(getSessionAttr("submitType"))) {
			redirect("/_l_3");
		} else if (Contans.URL_LOACTION_INTEGRATED.equals(getSessionAttr("submitType"))) {
			redirect("/_l_4");
		} else {
			redirect("/");
		}
		
	}

	/**
	 * 跳转管理会议室申请
	 */
	public void goManagerApplyRoom() {

		// setAttr("roomList", Room.dao.getAllRoom());

		// setAttr("ServerTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd
		// HH:mm:ss"));

		// setSessionAttr("messagecount",
		// UserMessageModel.dao.findByRecId(0).size());

		// setAttr("usermessage", UserMessageModel.dao.findByRecId(0));

		render("bookRoom_manager.jsp");
	}

	/**
	 * 跳转申请会议室
	 */
	public void goApplyRoom() {

		// setAttr("roomList", Room.dao.getAllRoom());

		setAttr("ServerTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		// List<UserMessageModel> mes =
		// UserMessageModel.dao.findByRecId((Integer)getSessionAttr("userId"));

		// setSessionAttr("messagecount", mes.size());

		// setAttr("usermessage", mes);

		render("bookRoom.jsp");
	}

	/**
	 * 跳转新增领导事记
	 */
	public void goLeadership() {

		// setAttr("leaderList", LeaderInfo.dao.getAllLeaderInfo());

		setAttr("ServerTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		render("leadership.jsp");
	}

	/**
	 * 跳转首页
	 */
	public void goIndex() {

		// setAttr("leaderSchedule",LeaderSchedule.dao.findAll());

		// setAttr("roomSchedule",RoomSchedule.dao.getTodayEvent());

		render("index.jsp");
	}

	/***************************************
	 * 表单跳转start*************************************8/ /** 跳转修改密码
	 */
	public void goEditpwd() {

		render("form/editPwd.jsp");
	}

	/**
	 * 跳转添加领导
	 */
	public void goAddLeader() {

		render("form/add_leader.jsp");
	}

	/**
	 * 跳转添加会议室
	 */
	public void goAddRoom() {

		render("form/add_room.jsp");
	}

	/**
	 * 跳转添加领导事记的表单
	 */
	public void goAddLeaderSchedule() {

		render("form/add_leaderSchedule.jsp");
	}

	/**
	 * 跳转修改领导事记申请表单
	 */
	public void goUpdLeaderSchedule() {

		render("form/upd_leaderSchedule.jsp");
	}

	/**
	 * 跳转添加会议室申请表单
	 */
	public void goAddRoomSchedule() {

		render("form/add_roomSchedule.jsp");
	}

	/**
	 * 跳转修改会议室申请表单
	 */
	public void goUpdRoomSchedule() {

		render("form/upd_roomSchedule.jsp");
	}

	/*************************************** 表单跳转end **************************************/

	/*************************************** 系统sysoa跳转start **************************************/
	/** 接待 **/
	public void goRecpUserIndex() {

		render("index_user.jsp");

	}

	public void goRecpManagerIndex() {

		render("index_manager.jsp");

	}

	/** 新闻 **/
	public void goNewsManagerIndex() {

		render("news_manager.jsp");

	}

	public void goNewsUserIndex() {

		render("news_user.jsp");

	}

	public void goNews1User() {

		render("news_list_user.jsp");

	}

	public void goNewsDetail() {
		setAttr("obj", AppModel.dao.findBy1Id(getParaToInt()));
		render("news_detail.jsp");
	}

	/** 督办 **/
	public void goSupervises() {

		render("supervise_list.jsp");
	}

	public void goaddSupervise() {

		setAttr("type", QPropertiesUtil.list(Contans.EVENT_SUPERVISE));
		setAttr("limit", QPropertiesUtil.list(Contans.HANDLE_LIMIT));
		setAttr("app_type", QPropertiesUtil.list(Contans.LEADER_APPROVAL_TYPE));
		setAttr("rec_organiz", QPropertiesUtil.list(Contans.REC_ORGANIZS));
		render("form/add_supervise_form.jsp");
	}

	public void goupdSupervise() {
		setAttr("type", QPropertiesUtil.list(Contans.EVENT_SUPERVISE));
		setAttr("limit", QPropertiesUtil.list(Contans.HANDLE_LIMIT));
		render("form/upd_supervise_form.jsp");
	}

	public void goaddRequire() {
		setAttr("supervise", SuperviseInfo.dao.find2All());
		setAttr("limit", QPropertiesUtil.list(Contans.HANDLE_LIMIT));
		render("form/add_require_form.jsp");
	}

	public void goaddProgress() {
		setAttr("complete", QPropertiesUtil.list(Contans.HANDLE_STATIUS));
		setAttr("report", QPropertiesUtil.list(Contans.REPORT_MATERIAL_TYPE));
		render("form/add_progress_info_form.jsp");
	}

	public void goSuperviseDetail() {

		Integer infoid = getParaToInt();

		SuperviseInfo sf = SuperviseInfo.dao.queryByINFOid(infoid);

		String ids = sf.getStr("require_organiz_id");

		String idstr = "";
		if (QStringUtil.countStr(ids, ",") > 0) {

			for (String id : ids.split(",")) {
				if (idstr.length() > 0)
					idstr += "、";
				idstr += UserOrganizModel.dao.findById(Integer.parseInt(id)).getStr("user_organiz_name");
			}
		} else {
			idstr = UserOrganizModel.dao.findById(Integer.parseInt(ids)).getStr("user_organiz_name");
			;
		}

		Page<SuperviseProgressInfo> page = SuperviseProgressInfo.dao.fingbyProId(CommonConstant.PAGENUMBER,
				CommonConstant.PAGESIZE, sf.getInt("progress_id"));

		setAttr("tab", getPara("tab"));
		setAttr("curl", getPara("curl"));
		
		setAttr("obj", sf);

		setAttr("str1", idstr);

		setAttr("page", page);

		render("supervise_detail.jsp");

	}

	public void gosuperviseprogressinfo() {
		render("form/apply_supervise_progress_info_form.jsp");
	}

	public void godoc() {

		String dirPath = PathKit.getWebRootPath() + "\\swf"; // pdf2swf.exe
																// 拷贝到附件路径行
		String fileName = PathKit.getWebRootPath() + "\\" + getPara("url");

		String outPath = new ConvertSwf().beginConvert(dirPath, fileName);

		outPath = outPath.substring(outPath.indexOf("swf"), outPath.length());

		outPath = outPath.replace("\\", "/");

		setAttr("file", outPath);

		render("views/FlexPaperViewer.jsp");
	}

	// 数据统计跳转
	public void gosupdatacond() {

		//查询类型
		setAttr("type", QPropertiesUtil.list(Contans.LEADER_APPROVAL_TYPE_U));
		render("form/supervise_data_cond.jsp");
	}
	
	//ie下载
	public void downloadIE(){
		String s1 =null;
		if("x64".equals(getPara())){
			s1 = "IE8-WindowsServer2003-x64-CHS.exe";
		}else if("x86".equals(getPara())){
			s1 = "IE8-WindowsXP-x86-CHS.exe";
		}
		String path =PathKit.getWebRootPath()+"/ie/"+s1;
		
		renderFile(path);
	}
    //数据备份
	public void dumpData(){
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("isSuccess", false);
		returnMap.put("msg", PropKit.get(Contans.OPATION_FAIL));
		try {
			String zippath = DumpMySql.backup();
			returnMap.put("isSuccess", true);
			returnMap.put("msg", PropKit.get(Contans.OPATION_SUCCESS));
			returnMap.put("zippath", zippath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderJson(returnMap);
	}
	//备份文件下载
	public void downloadDumpZip(){
		renderFile(getPara("zippath"));
	}
	// 综合审批
	public void gointegratedindex() {
		render("integrated_list.jsp");
	}

	public void goaddintegrated() {
		render("form/add_integrate_info_form.jsp");
	}

	public void gointegrateddetail() {
		render("form/detail_integrate_info_form.jsp");
	}

	public void gointegratedupd() {
		render("form/upd_integrate_info_form.jsp");
	}

	public void goadeditinfocon() {
		render("form/edit_info_con_form.jsp");
	}
	/*************************************** 系统sysoa跳转end **************************************/

	public void dicList() {
		List<ContansModel> li = null;

		String ss = getPara("code");
		String[] sr = null;

		if (Contans.CODE_HANDLE_LIMIT.equals(ss)) {

			/*
			 * li = CacheKit.get("PRO_CODE", "HANDLE_LIMIT", new IDataLoader() {
			 * public Object load() { return
			 * QPropertiesUtil.list(Contans.HANDLE_LIMIT); } });
			 */
			sr = Contans.HANDLE_LIMIT;

		} else if (Contans.CODE_HANDLE_STATIUS.equals(ss)) {

			sr = Contans.HANDLE_STATIUS;

		} else if (Contans.CODE_EVENT_SUPERVISE.equals(ss)) {

			sr = Contans.EVENT_SUPERVISE;

		} else if (Contans.CODE_APPROVAL.equals(ss)) {

			sr = Contans.APPROVAL;

		} else if (Contans.CODE_REPORT_MATERIAL_TYPE.equals(ss)) {

			sr = Contans.REPORT_MATERIAL_TYPE;

		} else if (Contans.CODE_REC_ORGANIZS.equals(ss)) {

			sr = Contans.REC_ORGANIZS;

		} else if (Contans.CODE_ORGANIZ_TYPE.equals(ss)) {

			sr = Contans.ORGANIZ_TYPE;

		}
		li = QPropertiesUtil.list(sr);

		renderJson(li);
	}

	public void dicOrganiz() {

		renderJson(UserOrganizModel.dao.findAll(null));

	}

	public void dicRecOrganiz() {

		renderJson(RecOrganiz.dao.findAll(null));

	}
}
